package org.skzr.chart4me.demo.box;

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * 中位数，平均值，边界值描述的数据集表达
 * @jdk 1.6
 * @author skzr.org(E-mail:skzr.org@gmail.com)
 * @version 1.0.0
 */
public class BoxAndWhiskerChartDemo1 extends ApplicationFrame {
	private static final long serialVersionUID = 1L;

	public BoxAndWhiskerChartDemo1(String s) {
		super(s);
		JPanel jpanel = createDemoPanel();
		jpanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(jpanel);
	}

	/**
	 * 创建3个Series， 5个Category的20个样本空间数据
	 * @return
	 */
	private static BoxAndWhiskerCategoryDataset createDataset() {
		DefaultBoxAndWhiskerCategoryDataset defaultboxandwhiskercategorydataset = new DefaultBoxAndWhiskerCategoryDataset();
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 5; j++) {
				java.util.List<?> list = createValueList(0.0D, 20D, 20);
				defaultboxandwhiskercategorydataset.add(list, "Series " + i,
						"Category " + j);
			}

		}

		return defaultboxandwhiskercategorydataset;
	}

	/**
	 * 构造数据
	 * @param d 最小值
	 * @param d1 最大值
	 * @param i 数据个数
	 * @return
	 */
	private static java.util.List<Double> createValueList(double d, double d1, int i) {
		ArrayList<Double> arraylist = new ArrayList<Double>();
		for (int j = 0; j < i; j++) {
			double d2 = d + Math.random() * (d1 - d);
			arraylist.add(new Double(d2));
		}

		return arraylist;
	}

	private static JFreeChart createChart( BoxAndWhiskerCategoryDataset boxandwhiskercategorydataset) {
		JFreeChart jfreechart = ChartFactory.createBoxAndWhiskerChart(
				"Box and Whisker Chart Demo 1", "Category", "Value",
				boxandwhiskercategorydataset, true);
		CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
		categoryplot.setDomainGridlinesVisible(true);
		categoryplot.setRangePannable(true);
		NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		return jfreechart;
	}

	public static JPanel createDemoPanel() {
		JFreeChart jfreechart = createChart(createDataset());
		ChartPanel chartpanel = new ChartPanel(jfreechart);
		chartpanel.setMouseWheelEnabled(true);
		return chartpanel;
	}

	public static void main(String args[]) {
		BoxAndWhiskerChartDemo1 boxandwhiskerchartdemo1 = new BoxAndWhiskerChartDemo1(
				"JFreeChart: BoxAndWhiskerChartDemo1.java");
		boxandwhiskerchartdemo1.pack();
		RefineryUtilities.centerFrameOnScreen(boxandwhiskerchartdemo1);
		boxandwhiskerchartdemo1.setVisible(true);
	}
}